/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "mysql_resultset.h"
#include <cassert>

namespace node {
    namespace rdb {

        MySQLResultSet::MySQLResultSet(MYSQL_RES* res)
            : ResultSet()
            , my_(res)
        {
            assert(res);

            field_num_ = mysql_num_fields(res);
            if (field_num_ == 0) return;
            row_ = new ::node::rdb::Field[field_num_];
            if (!row_) return;

            MYSQL_FIELD* rfields = mysql_fetch_fields(res);
            for (unsigned int x = 0; x < field_num_; ++x)
                row_[x].set_type(convertTypes(rfields[x].type));
        }

        MySQLResultSet::~MySQLResultSet(void)
        {
            Close();
        }

        bool MySQLResultSet::NextRow(void)
        {
            assert(my_);

            MYSQL_ROW row = mysql_fetch_row(my_);
            if (!row) {
                Close();
                return false;
            }

            for (unsigned int x = 0; x < field_num_; ++x) {
                row_[x].set_value(row[x]);
            }

            return true;
        }

        void MySQLResultSet::Close(void)
        {
            if (my_) {
                mysql_free_result(my_);
                my_ = NULL;
            }
            if (row_) {
                delete[] row_;
                row_ = NULL;
            }
        }

        ::node::rdb::Field::DataTypes MySQLResultSet::convertTypes(enum_field_types mysqlType) const
        {
            switch (mysqlType)
            {
            case FIELD_TYPE_TIMESTAMP:
            case FIELD_TYPE_DATE:
            case FIELD_TYPE_TIME:
            case FIELD_TYPE_DATETIME:
            case FIELD_TYPE_YEAR:
            case FIELD_TYPE_STRING:
            case FIELD_TYPE_VAR_STRING:
            case FIELD_TYPE_BLOB:
            case FIELD_TYPE_SET:
            case FIELD_TYPE_NULL:
                return ::node::rdb::Field::TYPE_STRING;
            case FIELD_TYPE_TINY:
            case FIELD_TYPE_SHORT:
            case FIELD_TYPE_LONG:
            case FIELD_TYPE_INT24:
            case FIELD_TYPE_LONGLONG:
            case FIELD_TYPE_ENUM:
                return ::node::rdb::Field::TYPE_INTEGER;
            case FIELD_TYPE_DECIMAL:
            case FIELD_TYPE_FLOAT:
            case FIELD_TYPE_DOUBLE:
                return ::node::rdb::Field::TYPE_FLOAT;
            default:
                return ::node::rdb::Field::TYPE_UNKNOWN;
            }
        }

    }
}